﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Windows.Forms;
using System.Diagnostics;
using System.Reflection;
using System.Net.NetworkInformation;
using DalMgr;
namespace ExcelAddIn2010
{
    public static class Funcs
    {
        public static bool IsFileInUse(string fileName)
        {
            bool inUse = true;

            FileStream fs = null;
            try
            {

                fs = new FileStream(fileName, FileMode.Open, FileAccess.Read,

                FileShare.None);

                inUse = false;
            }
            catch
            {

            }
            finally
            {
                if (fs != null)

                    fs.Close();
            }
            return inUse;//true表示正在使用,false没有使用
        }
        public static string GetFilePath(string pattern)
        {
            OpenFileDialog ofd = new OpenFileDialog();
            ofd.Filter = pattern;
            ofd.Multiselect = false;
            if (ofd.ShowDialog() == DialogResult.OK)
            {
                return ofd.FileName;
            }
            else
            {
                return "";
            }
        }
        public static Version GetSftVersion()
        {
            Assembly appAssem = Assembly.GetExecutingAssembly();
            return appAssem.GetName().Version;
        }
        /// <summary>
        /// 判断网络是否连接成功
        /// </summary>
        /// <returns></returns>
        public static bool CheckNetConnection()
        {
            try
            {
                Ping p = new Ping();
                PingReply pr = p.Send("www.baidu.com");
                if (pr.Status == IPStatus.Success)
                {
                    return true;
                }
            }
            catch
            {
            }
            return false;
        }
        public static bool IsANewDay()
        {
            //获取保存的日期，和当天日期比较，如果是今天日期，则表示已经检测过升级了，当天不再检测升级
            string strDate = SftDataMgr.ReadStr(gValue.SFT_CFG, gValue.SFT_CFG_TODAY);
            if (0 == string.Compare(strDate, DateTime.Today.ToString(gValue.STR_DATE_TIME_FORMAT)))
            {
                return false;
            }
            //如果不是当天日期，表示当天没有进行检测升级，需要检测升级，并写入当前日期
            SftDataMgr.WriteCfg(gValue.SFT_CFG, gValue.SFT_CFG_TODAY, DateTime.Today.ToString(gValue.STR_DATE_TIME_FORMAT));

            return true;
        }
    }
}
